//
//  DownloadFromInternet.m
//  X-Touch2
//
//  Created by shengchao yang on 12-3-23.
//  Copyright (c) 2012年 foxhis. All rights reserved.
//

#import "DownloadFromInternet.h"
#import "RegexKitLite.h"
#import "JSON.h"
#import "Http.h"
#import "Singleton.h"
#import "NSString+Additions.h"
#import "Common.h"

@implementation DownloadFromInternet
+ (id)downloadJSONData:(NSString *)url Error:(NSError **)outError{
	NSString *response = @"{\"code\":\"ok\",\"hotelName\":\"西软大酒店\"}"; 
    response = [Http httpGet:url Error:outError];
    response = [response stringByReplacingOccurrencesOfRegex:@"([{,]{1})(\\w+)(:)" withString:@"$1\"$2\"$3"];
    response = [response replaceFrom:@"'" To:@"$"];
	return [response JSONValue];
}
+ (void)downloadFile:(NSString *)url LocalFilePath:(NSString *)filePath{
	[Http httpDownload:url LocalFolder:filePath];
}

+ (BOOL)reportToCMS:(NSString *)udid{
    BOOL isSuccess = NO;
	NSString *url = [NSString stringWithFormat:@"%@/%@/%@",AppSession.downloadUrl,CMSUrl,udid];
    
    NSDictionary *array = [self downloadJSONData:url Error:nil];//获得Json字符串数组
    NSString * code = DictObject(array, @"code");
    NSString * hotelName = DictObject(array, @"hotelName");
    NSString * languages = DictObject(array, @"languages");
    MyNSLog(@"code=%@,hotelname=%@,languages=%@",code,hotelName,languages);
    
    if (code == nil || hotelName == nil ||languages == nil) {
        hotelName = AppCompany;
        isSuccess = NO;
    }else {
        code = [code lowercaseString];
        if ([code isEqualToString:@"ok"]||[code isEqualToString:@"true"]) {
            isSuccess = YES;
        }else {
            hotelName = AppCompany;
            languages = @"zh-Hans#en";
            isSuccess = NO;
        }
    }    
    
    [[NSUserDefaults standardUserDefaults] setObject:hotelName forKey:@"hotel_name"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    [[NSUserDefaults standardUserDefaults] setObject:languages forKey:@"support_langs"];
    [[NSUserDefaults standardUserDefaults] synchronize];
	return isSuccess;
}

+ (NSString *)sendMyOrders:(NSString *)sendOrderJson Error:(NSError **)outError {
    NSDictionary *dicJson=[NSDictionary dictionaryWithObjectsAndKeys:sendOrderJson,@"myorderjson", nil];//组装Dictionary
    NSString *url = [NSString stringWithFormat:@"%@/%@",AppSession.downloadUrl,SendMyOrderUrl];
    return [Http httpPost:url Post:dicJson Error:outError];
}
+ (NSString *)sendMyOrders1:(NSString *)sendOrderJson Error:(NSError **)outError {
    NSDictionary *dicJson=[NSDictionary dictionaryWithObjectsAndKeys:sendOrderJson,@"myorderjson", nil];//组装Dictionary
    NSString *url = [NSString stringWithFormat:@"%@/%@",AppSession.downloadUrl,SendMyOrderUrl1];
    return [Http httpPost:url Post:dicJson Error:outError];
}
@end
